package com.banzhi.shop.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banzhi.shop.R;
import com.banzhi.shop.activity.AddAddressActivity;
import com.banzhi.shop.entity.ChoiceAddress.ChoiceAddress_Data;

import java.util.List;

/**
 * Created by LeiHe on 2017/4/26.
 */
public class ChoiceAddressAdapter extends BaseAdapter<ChoiceAddress_Data> {
    private setDefaultAddressCallBack callBack;

    public ChoiceAddressAdapter(Context context, List<ChoiceAddress_Data> data) {
        super(context, data);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ChoiceAddress_Data data = getData().get(position);
        viewHolder holder = null;
        if (convertView == null) {
            holder = new viewHolder();
            convertView = getInflater().inflate(R.layout.item_address, null);
            holder.ivcheck = (ImageView) convertView.findViewById(R.id.iv_check);
            holder.tvredact = (TextView) convertView.findViewById(R.id.tvredact);
            holder.tvname = (TextView) convertView.findViewById(R.id.tv_name);
            holder.tvphone = (TextView) convertView.findViewById(R.id.tv_phone);
            holder.tvaddress = (TextView) convertView.findViewById(R.id.tv_address);
            holder.tvdeletet = (TextView) convertView.findViewById(R.id.tvdeletet);
            convertView.setTag(holder);
        }
        holder = (viewHolder) convertView.getTag();
        if ("1".equals(data.getIsdefault())) {
            holder.ivcheck.setImageResource(R.mipmap.xuanzhe2);
        } else {
            holder.ivcheck.setImageResource(R.mipmap.dizhibuxuanzhe);
            holder.ivcheck.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    callBack.setDefaultAddress(data.getId(), position);
                }
            });
        }
        holder.tvdeletet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopupwindow(data.getId(), position);

            }
        });
        holder.tvname.setText(data.getRealname());
        holder.tvphone.setText(data.getMobile());
        holder.tvaddress.setText(data.getProvince() + data.getCity() + data.getArea() + data.getAddress());
        holder.tvredact.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), AddAddressActivity.class);
                intent.putExtra("data", data);
                getContext().startActivity(intent);
            }
        });
        return convertView;
    }

    class viewHolder {
        ImageView ivcheck;
        TextView tvredact;
        TextView tvname;
        TextView tvphone;
        TextView tvaddress;
        TextView tvdeletet;
    }

    public interface setDefaultAddressCallBack {
        void setDefaultAddress(String id, int position);

        void deleteAddress(String id, int position);
    }

    public void setCallBack(setDefaultAddressCallBack callBack) {
        this.callBack = callBack;
    }

    /**
     * 点击删除提示下是不是确定删除地址
     *
     * @param id
     * @param position
     */
    private void showPopupwindow(final String id, final int position) {
        //设置contentView
        View contentView = LayoutInflater.from(getContext()).inflate(R.layout.popupwindow_delete_address, null);

        int width = getContext().getResources().getDisplayMetrics().widthPixels;
        int height = getContext().getResources().getDisplayMetrics().heightPixels;

        final PopupWindow popWindow = new PopupWindow(contentView, width, height);
        popWindow.setAnimationStyle(R.style.AnimBottom);

        popWindow.setFocusable(true);
        popWindow.setOutsideTouchable(false);// 设置允许在外点击消失
        //设置各个控件的点击响应
        TextView tvcancle = (TextView) contentView.findViewById(R.id.tv_cancle);
        TextView tvtrue = (TextView) contentView.findViewById(R.id.tv_true);
        RelativeLayout rl = (RelativeLayout) contentView.findViewById(R.id.rl_background);
        LinearLayout ll = (LinearLayout) contentView.findViewById(R.id.ll);
        rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow.dismiss();
            }
        });
        ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        tvcancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow.dismiss();
            }
        });
        tvtrue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.deleteAddress(id, position);
                popWindow.dismiss();
            }
        });
        popWindow.showAtLocation(contentView, Gravity.CENTER | Gravity.CENTER_HORIZONTAL, 0, 0);
    }
}
